package src.DAO;
import src.jdbc.PostgreSQL;
import src.Entities.Egresado;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Patty
 */
public class EgresadoDAO {
    
   public String deleteEgresado(int egresadoId){
        try {
            String sql = "DELETE FROM egresado WHERE egresado_id = ?;";
            PreparedStatement delete = PostgreSQL.getConnection().prepareStatement(sql);
            delete.setInt(1, egresadoId);
            return String.valueOf(delete.execute());
        } catch (Exception e) {
            return e.toString();
        }
    }

    public String insertEgresado(Egresado egresado) {
        try {
            String sql = "INSERT " +
                    "       INTO egresado(egresado_id, run, dv, nombre, app_paterno, app_materno)" +
                    "     VALUES (NEXTVAL('egresado_seq'), ?, ?, ?, ?, ?, ?)";
            PreparedStatement insert = PostgreSQL.getConnection().prepareStatement(sql);
            insert.setInt(1, egresado.getEgresadoId());
            insert.setString(2, egresado.getRun());
            insert.setString(3, String.valueOf(egresado.getDv()));
            insert.setString(4, egresado.getNombre());
            insert.setString(5, egresado.getAppPaterno());
            insert.setString(6, egresado.getAppMaterno());
            return String.valueOf(insert.execute());
        } catch (Exception e) {
            return e.toString();
        }
    }

    public ArrayList selectEgresado(String sql) {
        ArrayList results = new ArrayList();
        try {
            Connection con = PostgreSQL.getConnection();
            Statement select = con.createStatement();
            ResultSet query = select.executeQuery(sql);
            while (query.next()) {
                Egresado egresado= new Egresado(query.getInt("egresado_id"), 
                        query.getString("run"), 
                        query.getString("dv").charAt(0),
                        query.getString("nombre"),
                        query.getString("app_paterno"), query.getString("app_materno"));
                results.add(egresado);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return results;
    }

    public String updateEgresado(Egresado egresado) {
        try {
            String sql = "UPDATE egresado" +
                    "        SET run = ?, dv = ?, nombre = ?, app_paterno = ?, app_materno = ?" +
                    "      WHERE egresado_id = ?";
            PreparedStatement update = PostgreSQL.getConnection().prepareStatement(sql);
            update.setString(1, egresado.getRun());
            update.setString(2, String.valueOf(egresado.getDv()));
            update.setString(3, egresado.getNombre());
            update.setString(4, egresado.getAppPaterno());
            update.setString(5, egresado.getAppMaterno());
            update.setInt(6, egresado.getEgresadoId());
           return String.valueOf(update.execute());
        } catch (Exception e) {
            return e.toString();
        }
    }
}
